/*
Copyright (C) 2021-2025 Casa Xu (also Zhiyan Xu) from HIT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/*
 * LOS_GNSS.h
 *
 *  Created on: 2022年7月17日
 *      Author: 64435
 */

#ifndef PERIPHERAL_LAYER_INC_LOS_GNSS_H_
#define PERIPHERAL_LAYER_INC_LOS_GNSS_H_

#include "LOS_CAN.h"

typedef struct LOS_GNSSSTA_t
{
    /* 健康管理状态量 */
    uint8_t VAL;                 //数据有效性标志，0：有效， 其他值：无效
    uint8_t CMD;                 //开关机指令状态,发送开机指令后置1，发送关机指令后置0
    uint8_t PWR;                 //电源状态，读取电源遥测，通电为1，断电为0
    uint8_t MSG;                 //通信状态，通信正常为0，通信异常为其他值
    uint8_t SWNUM;
    uint8_t RBCNT;               //复位次数
    uint8_t NFR;                 //Need For Reset 需要复位标志位，1为需要复位，0为不需要
    uint8_t RBT_F;              //表示单机是否处于重启过程中 0：未处于 1：处于重启过程中
    uint8_t RECOV;               //恢复计数：单机重启完成后，需要等待一段时间才能再次进入重启流程，将该计数置于某一值开始倒计时，倒计时结束后会再次开始判断是否需要重启
    uint8_t REC;                //CAN识别标志位,0:未识别  1：识别
    uint8_t HL_TXF;              //HL_前缀表示健康管理相关变量。遥测请求发送完成标志,发送一次后该量置1，接收到回应后置0(TX FINISHED)
    uint8_t HL_WCNT;             //等待接收计数器。该计数器在操作系统的一个线程中累加，当HL_TXF为1时该值+1，HL_TXF为0时该值清0，当累加到一定值时系统会判断通信异常，并将MSG置1
    uint16_t FlagF_CNT;         //物理量异常重启计数
    uint16_t FlagT_CNT;         //通信异常重启计数
    uint16_t FlagY_CNT;         //
    uint32_t TMCNT_RADIO;             //遥测计数
    /* GNSS定位数据 */
    uint32_t sec;                //来自GNSS定位数据广播的UTC累计秒整数
    float pos_vel[6];           //xyz轴方向 xyz轴速度
    uint32_t pos_vel_o[6];      //位置速度原码
    float orbit[6];             //轨道六根数：半长轴、偏心率、倾角、升交点赤经、近心点角距、平近点角
    uint32_t orbit_o[6];      //轨道六根数原码
    uint8_t pos_flag;            //定位标记 3：非定位  9：定位
    float GNSS_Time;            //GNSS时间
    uint8_t Flag_D;               //aocs需要的Flag_D
    uint8_t pos_type;           //定位类型

    uint32_t sec_temp;                //来自GNSS定位数据广播的UTC累计秒整数
    float pos_vel_temp[6];           //xyz轴方向 xyz轴速度
    float orbit_temp[6];             //轨道六根数：半长轴、偏心率、倾角、升交点赤经、近心点角距、平近点角
    uint8_t pos_flag_temp;            //定位标记 3：非定位  9：定位
    float GNSS_Time_temp;            //GNSS时间
    uint8_t pos_type_temp;           //定位类型

}LOS_GNSSSTA_t;

typedef struct LOS_GNSSTEST_t
{
    uint8_t pos_sel;  // 0:恢复/不操作  1:X轴  2:Y轴  3:Z轴  4:改变生效
    uint8_t vel_sel;  // 0:恢复/不操作  1:X轴  2:Y轴  3:Z轴  4:改变生效
    uint8_t six_sel;  // 0:恢复/不操作  1:半长轴  2:偏心率  3:倾角  4:升交点赤经  5:近心点角距  6:平近点角  7:改变生效
    uint8_t mode_sel; // 0:恢复/不操作  1:改变生效
    float pos_upt[3];
    float vel_upt[3];
    float six_upt[3];
    uint8_t mode_upt;
}LOS_GNSSTEST_t;

extern LOS_GNSSSTA_t STA_GNSS;
LOS_GNSSTEST_t TEST_GNSS;

extern uint8_t GNSS_CMD_TimePolling[3];    //时间数据轮询
extern uint8_t GNSS_CMD_StatusPolling[3];  //基本状态轮询
extern uint8_t GNSS_CMD_OriginPolling[3];  //原始观测量轮询
extern uint8_t GNSS_CMD_Reset[4];          //GNSS复位

extern uint8_t BUF_GNSS_OriginData[511];
extern uint8_t FLAG_GNSS_OriginData;

extern uint8_t TMPACK_GNSSBasic[161];     //基本遥测
extern uint8_t TMPACK_GNSSRadio[64];      //定位广播
extern uint8_t TMPACK_GNSSEphemeris[114]; //星历数据

void LOS_GNSS_CMD(uint8_t* cmd);
void LOS_CAN_GNSSCallBack(LOS_CANMSG_t* can_re);

void LOS_GNSS_PWROn();
void LOS_GNSS_PWROff();

#endif /* PERIPHERAL_LAYER_INC_LOS_GNSS_H_ */
